package com.fudan.framework.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fudan.framework.entity.RequestEntity;
import com.fudan.framework.entity.ResponseMsg;
import com.fudan.framework.utils.EntityUtils;

/**
 * 功能性接口，继承该接口使得实体T拥有增加的接口
 * @author Xuxinxing
 *
 * @param <T>
 */
public interface AddController<T extends RequestEntity> extends FunctionalController<T>{
	
    @RequestMapping("/add")
    @ResponseBody
    default public ResponseMsg doAdd(HttpServletRequest request) {
        try {
            //checkPermission("add");
            boolean result = add(request);
            if (result) {
                return ResponseMsg.build(SUCCESS_CODE, SUCCESS);
            } else {
                return ResponseMsg.build(ADD_ERROR_CODE, ERROR);
            }
        } catch (Exception e) {
        	e.printStackTrace();
            return ResponseMsg.build(SYSTEM_ERROR_CODE, e.getMessage());
        }
    }
    
    default public Boolean add(HttpServletRequest request) throws Exception {
        T entity = buildEntity(request);
        boolean result = injectService().add(entity);
        return result;
    }
    default public T buildEntity(HttpServletRequest request) throws Exception{
    	T entity = EntityUtils.toEntity(request, getActualClass(AddController.class));
    	return entity;
    }
}
